
import datetime
import os
import time
import json
import numpy as np
import pandas as pd
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
file_path = r'C:\Users\huajia\Desktop\rqalpha3\rqalpha\apis'
files = os.listdir(file_path)
# print(files)

t_li = {}
for file in files:
    temp = os.path.getmtime(os.path.join(file_path, file))
    temp = time.localtime(temp)
    temp = time.strftime('%Y-%m-%d %H:%M:%S', temp)
    t_li[file] = temp
max_t = sorted(t_li.values())[-2:]
need_files = []
for i in max_t:
    file = [key for key, value in t_li.items() if value == i][0]
    need_files.append(file)


def match_orders_to_df(events):
    items = []
    for event in events:
        args = event["order_record"]
        # print(args)
        new_item = {
            "direction": args["args_"]["direction"],
            "price": args["price_"],
            "midp": args["args_"]["remark"]['price'] if 'price' in args["args_"]["remark"].keys() else np.nan,
            "amount": args["amount_"],
            "L": args["args_"]["remark"]['long_trade_id'] if 'long_trade_id' in args["args_"]["remark"].keys() else np.nan,
            "S": args["args_"]["remark"]['short_trade_id'] if 'short_trade_id' in args["args_"]["remark"].keys() else np.nan,
            "remark": ", ".join(
                [f"{k}:{v}" for k, v in args["args_"]["remark"].items()]
            )
            if args["args_"]["remark"]
            else "",
            "created_at": datetime.datetime.fromtimestamp(
                event["created_at"] / 1000.0) - datetime.timedelta(hours=0 if True else 8),
            }
        items.append(new_item)
    return pd.DataFrame.from_records(items)


result_li = []
for file in need_files:
    with open(os.path.join(file_path, file)) as f:
        data = json.load(f)
        send_order_events = data["send_order_events"]
        match_events = data["match_events"]
        match_orders_df = match_orders_to_df(match_events)
        result_li.append(match_orders_df)
result_df = pd.concat(result_li).sort_values(by=['created_at'])
save_path = os.path.join(file_path, 'result.json')
if os.path.exists(save_path):
    os.remove(save_path)
result_df_json = result_df.to_json(path_or_buf=save_path, orient="records")



# with open(r'C:\Users\huajia\Desktop\20220406_100658-DC_CTA.json') as f:
#     data = json.load(f)
#     send_order_events = data["send_order_events"]
#     match_events = data["match_events"]
#     match_orders_df = match_orders_to_df(match_events)
#     save_path = os.path.join(file_path, 'result.json')
#     if os.path.exists(save_path):
#         os.remove(save_path)
#     match_orders_json = match_orders_df.to_json(path_or_buf=save_path, orient="records")
#     print(match_orders_df.head(10))
#     print(match_orders_json)

# {'args_': {'direction': 'Sell', 'exname': 'Binance', 'account': 'Huobi1',
#            'symbol': 'EthBtc', 'price': 46901.01, 'amount': 1e-05,
#            'turnover': 0.0, 'maker': False, 'hidden': False,
#            'remark': {'long_trade_id': 251, 'time': 1630456618000}},
#  'strategy_': 'Strategy1', 'uuid_': '7849f45e4c8444838067b7467a425426',
#  'price_': 46901.01, 'amount_': 1e-05, 'time_': 1630456618153, 'created_time_': 1630456618000, 'status_': 'Closed'}
#
